package model;

import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.rowset.serial.SerialBlob;

import dbcTools.DB_Operation;

public class GroupMessageDBTO {
	private final static String tableName = "group_message";
	
	private DB_Operation dbOperation;
	
	public GroupMessageDBTO(Connection connection) {
		this.dbOperation = new DB_Operation(connection, GroupMessageDBTO.tableName);
	}
	
	public void setConnection(Connection connection) {
		this.dbOperation.setConnection(connection);
	}
	public void setAutoCommit(Boolean autoCommit) throws SQLException {
		this.dbOperation.setAutoCommit(autoCommit);
	}
	public void commit() throws SQLException {
		this.dbOperation.commit();
	}

	public int addNewGroupMessage(int group_id, int user_id, String content_type, String content) throws SQLException {
		dbOperation.prepareNextOperation();
		Blob contentBlob = new SerialBlob(content.getBytes());
		return dbOperation
			.insert("group_id", group_id)
			.insert("creation_time", System.currentTimeMillis())
			.insert("user_id", user_id)
			.insert("content_type", content_type)
			.insert("content", contentBlob)
			.executeUpdate();
	}
	public int deleteGroupMessage(int group_id, long creation_time) throws SQLException {
		dbOperation.prepareNextOperation();
		return dbOperation.delete()
				.where("group_id", "=", group_id)
				.and("creation_time", "=", creation_time)
				.executeUpdate();
	}
}
